﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class ChungChiProvider
    {
        public static List<ChungChi> LayDanhSachChungChi()
        {
            var lst = new List<ChungChi>();
            using (var context = new QLTTTHEntities())
            {
                var query = (from u in context.ChungChis select u);

                foreach (var row in query)
                {
                    var cchi = new ChungChi();
                    cchi.MaChungChi = row.MaChungChi;
                    cchi.MaMH = row.MaMH;
                    cchi.TenChungChi = row.TenChungChi;
                    cchi.NoiCap = row.NoiCap;
                    cchi.ThoiHan = row.ThoiHan;
                    lst.Add(cchi);
                }
            }

            return lst;
        }


        public static int ThemChungChi(ChungChi chungchi)
        {
            int maChungChi = -1;
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    context.ChungChis.Add(chungchi);
                    context.SaveChanges();
                    maChungChi = chungchi.MaChungChi;
                }
            }
            catch
            {

            }
            return maChungChi;
        }



        public static bool XoaChungChi(int maChungChi)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    var query = (from u in context.ChungChis
                                 where u.MaChungChi.Equals(maChungChi)
                                 select u).First();

                    context.ChungChis.Remove(query);
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool CapNhatChungChi(ChungChi chungchi)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    var query = (from u in context.ChungChis
                                 where u.MaChungChi.Equals(chungchi.MaChungChi)
                                 select u).First();

                    query.TenChungChi = chungchi.TenChungChi;
                    query.ThoiHan = chungchi.ThoiHan;
                    query.NoiCap = chungchi.NoiCap;
                    query.MaMH = chungchi.MaMH;
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
